package org.iabako.client.ui.form.business.util;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.SuggestOracle;
import org.iabako.client.ui.form.business.SearchForm;

import java.util.Date;

/**
 * Created by jose on 04/09/14.
 */
public class SearchActionHandler implements ValueChangeHandler<Date>, ClickHandler, BlurHandler, ChangeHandler,
        KeyUpHandler, FocusHandler, SelectionHandler<SuggestOracle.Suggestion> {

    SearchForm searchForm;

    public SearchActionHandler(SearchForm searchForm) {

        this.searchForm = searchForm;
    }

    public void onSelection(SelectionEvent<SuggestOracle.Suggestion> event) {
        searchForm.search();
    }

    public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
            event.getNativeEvent().stopPropagation();
            searchForm.search();
            searchForm.suggestInputText.getIabakoSuggestOracle().setEnabled(false);
            searchForm.suggestInputText.getInputField().setFocus(false);
        }
    }

    public void onFocus(FocusEvent event) {
        searchForm.suggestInputText.getIabakoSuggestOracle().setEnabled(true);
    }

    public void onBlur(BlurEvent event) {
        searchForm.search();
    }

    public void onChange(ChangeEvent event) {
        searchForm.search();
    }

    public void onClick(ClickEvent event) {
        searchForm.search();
    }

    public void onValueChange(ValueChangeEvent<Date> event) {
        searchForm.search();
    }
}
